<?php
$objRoleForm = new BM_PHPForm();
$objRoleForm->UseLocator();
$objRoleForm->SetIndent("          ");

if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "err"){
	$strRole = $_REQUEST['role'];
	$objRoleForm->SetError("delete","Some Part Of Sources Depends On [$strRole], You Cann't Delete !");
}
if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "inf"){
	$strRole = $_REQUEST['role'];
	$objRoleForm->SetInfor("delete","Role [$strRole] Deleted Successfully!");
}
if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "add"){
	$strRole = $_REQUEST['role'];
	$objRoleForm->SetInfor("roleadd","Role [$strRole] Added Successfully!");
}

if ($action == "update"){
	$entRole = RoleController::find($id);
}

if ($objRoleForm->IsPost()){
	if ($action == "update"){
		$objRoleForm->ChEmptiness("name", "Name is empty.");
		$objRoleForm->ChWithRegex("name", "Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
		$objRoleForm->ChMaxLength("name", "Name must not be more than 40 chars.", 40);
	}else{
		if (($objRoleForm->ChWithRegex("id", "ID Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/"))){
			$entRole0 = RoleController::find($objRoleForm->arrPostData['id']);
			$objRoleForm->ChCustomVal("id", "ID Exist ! Please Choose Another ." , $entRole0->id === NULL);	
		}
		$objRoleForm->ChEmptiness("id", "ID is empty.");
		$objRoleForm->ChMaxLength("id", "ID must not be more than 40 chars.", 40);
		$objRoleForm->ChWithRegex("id", "ID Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
		$objRoleForm->ChEmptiness("name", "Name is empty.");
		$objRoleForm->ChMaxLength("name", "Name must not be more than 40 chars.", 40);
		$objRoleForm->ChWithRegex("name", "Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
	}
	
	if ($objRoleForm->IsValid()){
		if ($action == "update"){
			$entRole = new Role($objRoleForm->arrPostData['id'],$objRoleForm->arrPostData['name']);
			RoleController::update($entRole);
			$objRoleForm->SetInfor("submit","Role Updated Successfully !");
		}else{
			$entRole = new Role($objRoleForm->arrPostData['id'],$objRoleForm->arrPostData['name']);
			RoleController::save($entRole);
			$objRoleForm->SetInfor("submit","Role Added Successfully !");
			$action= "update";			
		}
	}	
}

print $objRoleForm->ErrMsgs();
print $objRoleForm->InfMsgs();
if ($action == "update"){
	print $objRoleForm->Open("roleform","Role Update Form");
	print $objRoleForm->HiddenField("action" ,"$action");
	print $objRoleForm->TextBox("id" ,"ID:","$entRole->id",array('readonly'=>'readonly'));
	print $objRoleForm->TextBox("name" ,"Name_:","$entRole->name");
	print $objRoleForm->SubmitButton("submit", "Update", array(), "submit");
}else{
	print $objRoleForm->Open("roleform","Role Registration Form");
	print $objRoleForm->HiddenField("action" ,"$action");
	print $objRoleForm->TextBox("id" ,"ID:","");
	print $objRoleForm->TextBox("name" ,"Name_:","");
	print $objRoleForm->SubmitButton("submit", "Submit", array(), "submit");
}
print $objRoleForm->Close();
?>